import axios from 'axios';
import {ElMessage} from "element-plus";

const request = axios.create({
    baseURL: '/api',
    timeout: 5000,
})

request.interceptors.request.use((config)=>{
    // config.headers.a = 111
    // console.log("config", config);
    return config;
})

request.interceptors.response.use((response)=>{
    // success
    // console.log("response", response);
    return response;
}, error => {
    // failure
    const status = error.response.status;
    switch (status) {
        case 400:
            ElMessage.error({
                type: 'error',
                message: '请求参数有误'
            })
            break;
        case 401:
            ElMessage.error({
                type: 'error',
                message: 'unauthorized'
            })
            break;
        case 403:
            ElMessage.error({
                type: 'error',
                message: 'forbidden'
            })
            break;
        case 404:
            ElMessage.error({
                type: 'error',
                message: error.response.message
            })
            break;
        case 501|502|503|504|505|506|507:
            ElMessage.error({
                type: 'error',
                message: '系统异常'
            })
            break;
    }
    return Promise.reject(new Error(error.message));
})

export default request;
